<?php

namespace App\Http\Controllers\Admin;

use App\Models\AdminUsers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use DB;

class IndexController extends Controller
{
    public function index(){
        $user = Auth::guard('admin')->user();
        $menulist = $this->getMenu();
        //$menulist = getAllMenu();
        return view('admin.admin', compact('user', 'menulist'));
    }

    public function home(){
        /*商户总数*/
        $total = DB::table('users')->whereIn('status',[-1,2,4,6])->count();
        /*待审核数*/
        $uncheckSum = DB::table('users')->whereIn('status',[1,3,5])->count();
        /*今日已审批数*/
        $todayCheckPass = DB::table('users')->whereDate('updated_at',date('Y-m-d',time()))->whereIn('status',[-1,2,4,6])->count();
        $begin=date('Y-m-d',time());
        $whereDate ="erp_users.updated_at like '$begin%' ";
        /*本月新增用户*/
        $now = Carbon::now();
        $dt = Carbon::parse($now);
        $aMoonAdd = DB::table('users')->whereMonth('created_at',$dt->month)->whereIn('status',[-1,2,4,6])->count();
        $whereMonth = "erp_users.created_at like '%$dt->month%'";
        //当月新增的会员的数量
        $current_month_added_members_num = DB::table('member')->whereMonth('create_time',$dt->month)->count();


        return view('admin.index.home',compact('total','uncheckSum','todayCheckPass','aMoonAdd','current_month_added_members_num','whereDate','whereMonth'));
    }

    public function getMenu(){
        $menulist= getAllMenu();
        $user = Auth::guard('admin')->user();
        foreach ($menulist as $key => $value){
            if ($user->can($key)){
                foreach ($value['sub_menu'] as $k => $v){
                    if (!$user->can($k)){
                        unset($menulist[$key]['sub_menu'][$k]);
                    }
                }
            }else{
                unset($menulist[$key]);
            }
        }
        return $menulist;
    }

//今日新增商户
// 今日已审批数
//待审核数

}
